package com.brian.common.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;

import com.brian.common.tools.Env;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * ResourceUtil
 *
 * @author Trinea 2012-5-26
 */
public class ResourceUtil {

    /**
     * get an asset using ACCESS_STREAMING mode. This provides access to files that have been bundled with an
     * application as assets -- that is, files placed in to the "assets" directory.
     *
     * @param context
     * @param fileName The name of the asset to open. This name can be hierarchical.
     * @return
     */
    public static String geFileFromAssets(Context context, String fileName) {
        if (context == null || StringUtil.isEmpty(fileName)) {
            return null;
        }

        StringBuilder s = new StringBuilder("");
        try {
            InputStreamReader in = new InputStreamReader(context.getResources().getAssets().open(fileName));
            BufferedReader br = new BufferedReader(in);
            String line;
            while ((line = br.readLine()) != null) {
                s.append(line);
            }
            return s.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * get a data stream for reading a raw resource. This can only be used with resources whose value is the name of an
     * asset files -- that is, it can be used to open drawable, sound, and raw resources; it will fail on string and
     * color resources.
     *
     * @param context
     * @param resId   The resource identifier to open, as generated by the appt tool.
     * @return
     */
    public static String geFileFromRaw(Context context, int resId) {
        if (context == null) {
            return null;
        }

        StringBuilder s = new StringBuilder();
        try {
            InputStreamReader in = new InputStreamReader(context.getResources().openRawResource(resId));
            BufferedReader br = new BufferedReader(in);
            String line;
            while ((line = br.readLine()) != null) {
                s.append(line);
            }
            return s.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getString(int resid, Object... formatArgs){
        return Env.getContext().getString(resid, formatArgs);
    }

    public static String getString(Context context, int resId) {
        if (context == null) {
            return "";
        }
        return context.getResources().getString(resId);
    }

    /**
     * 根据资源ID返回字符串
     *
     * @param resID
     * @return
     */
    public static final String getString(int resID) {
        return Env.getContext().getString(resID);
    }

    /**
     * 获取颜色
     */
    public static final int getColor(int resid) {
        return Env.getContext().getResources().getColor(resid);
    }

    /**
     * 获取图片
     */
    public static final Drawable getDrawable(int resid) {
        return Env.getContext().getResources().getDrawable(resid);
    }

    /**
     * 获取dimems大小（单位为像素pix）
     *
     * @param resid
     * @return
     */
    public static final int getDimens(int resid) {
        return (int) Env.getContext().getResources().getDimension(resid);
    }

    /**
     * 根据资源id返回字符串数组
     *
     * @param resid
     * @return
     */
    public static final String[] getStringArray(int resid) {
        return Env.getContext().getResources().getStringArray(resid);
    }

    /**
     * 获取资源中的整数数据
     *
     * @param resid
     * @return
     */
    public static final int getInteger(int resid) {
        return Env.getContext().getResources().getInteger(resid);
    }

    /**
     * 根据资源名获取资源id
     *
     * @param name 资源名
     * @return
     */
    public static final int getDrawableResId(String name) {
        return Env.getContext().getResources().getIdentifier(name, "drawable", Env.getContext().getPackageName());
    }

}
